package cn.ycgogo.org.controller;

import cn.ycgogo.org.util.AjaxResult;
import cn.ycgogo.org.util.PageList;
import cn.ycgogo.org.domain.Department;
import cn.ycgogo.org.query.DepartmentQuery;
import cn.ycgogo.org.service.DepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

/**
 * The type Department controller.
 * 部门接口
 *
 * @author Yang
 * @date 2023 /1/12 10:04
 */
@RestController
@RequestMapping("/department")
@Api(value = "部门接口类",description = "处理部门的CRUD操作")
public class DepartmentController {
    @Resource
    private DepartmentService service;

    /**
     * Find one department.
     * 查询单个部门对象
     *
     * @param id the id
     * @return the department
     */
    @GetMapping("/{id}")
    @ApiOperation(value = "接口:查询单个对象")
    public Department findOne(@PathVariable("id")Long id){
        return service.loadById(id);
    }

    /**
     * Find all list.
     * 查询所有部门对象
     *
     * @return the list
     */
    @GetMapping
    @ApiOperation(value = "接口:查询所有对象")
    public List<Department> findAll(){
        return service.loadAll();
    }

    /**
     * Delete ajax result.
     * 删除单个部门对象
     *
     * @param id the id
     * @return the ajax result
     */
    @DeleteMapping("/{id}")
    @ApiOperation(value = "接口:删除单个对象")
    public AjaxResult delete(@PathVariable("id") Long id){
        try {
            service.remove(id);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
           return new AjaxResult("删除失败",false);
        }
    }

    /**
     * Add or update ajax result.
     * 添加或修改单个部门对象
     *
     * @param department the department
     * @return the ajax result
     */
    @PutMapping
    @ApiOperation(value = "接口:添加单个对象或修改单个对象")
    public AjaxResult addOrUpdate(@RequestBody Department department){
        try {
            if (department.getId() == null) {
                service.save(department);
                return  new AjaxResult();
            }else {
                service.update(department);
                return  new AjaxResult();
            }
        } catch (Exception e) {
            e.printStackTrace();
            return  new AjaxResult("操作失败",false);
        }
    }

    /**
     * Query page page list.
     * 部门分页数据
     *
     * @param query the query
     * @return the page list
     */
    @PostMapping
    @ApiOperation(value = "接口:查询分页对象")
    public PageList<Department> queryPage(@RequestBody DepartmentQuery query){
            return service.queryPage(query);
    }

    /**
     * Patch delete ajax result.
     * 批量删除部门对象
     * @param ids the ids
     * @return the ajax result
     */
    @PatchMapping
    public AjaxResult patchDelete(@RequestBody List<Long> ids){
        try {
            service.patchDelete(ids);
            return new AjaxResult();
        } catch (Exception e) {
            e.printStackTrace();
            return new AjaxResult("删除失败",false);
        }
    }

    /**
     * Dept tree list.
     * 级联菜单,查询部门树
     * @return the list
     */
    @ApiOperation(value = "接口：查询部门树")
    @GetMapping("/deptTree")
    public List<Department> deptTree(){
        return service.deptTree();
    }
}
